﻿namespace Microsoft.Xna.Framework
{
    using System;

    public class FactCubeCamera
    {
        public static Matrix CreateProjectionMatrix(float width, float height, float fov, float near, float far)
        {
            float num = width / height;
            float num2 = near;
            float num3 = far;
            float num4 = (float) Math.Tan(DegreesToRadians((double) fov) / 2.0);
            float num5 = num / num4;
            float num6 = 1f / num4;
            float num7 = (num3 != double.PositiveInfinity) ? (num3 / (num2 - num3)) : -1f;
            return new Matrix(num6, 0f, 0f, 0f, 0f, num5, 0f, 0f, 0f, 0f, num7, -1f, 0f, 0f, num2 * num7, 0f);
        }

        public static Matrix CreateViewMatrix(Vector3 position, Vector3 lookDirection, Vector3 upDirection)
        {
            Vector3 vector = -lookDirection;
            vector.Normalize();
            Vector3 vector2 = Vector3.Cross(upDirection, vector);
            vector2.Normalize();
            Vector3 vector3 = Vector3.Cross(vector, vector2);
            Vector3 vector4 = position;
            float num = -Vector3.Dot(vector2, vector4);
            float num2 = -Vector3.Dot(vector3, vector4);
            return new Matrix(vector2.X, vector3.X, vector.X, 0f, vector2.Y, vector3.Y, vector.Y, 0f, vector2.Z, vector3.Z, vector.Z, 0f, num, num2, -Vector3.Dot(vector, vector4), 1f);
        }

        internal static double DegreesToRadians(double degrees)
        {
            return (degrees * 0.017453292519943295);
        }

        public static Matrix GetDisplayMatrix(Matrix view, Matrix projection, float width, float height)
        {
            return ((view * projection) * GetHomogeneousToViewportTransform3D(width, height));
        }

        public static Matrix GetHomogeneousToViewportTransform3D(float width, float height)
        {
            float num = width / 2f;
            float num2 = height / 2f;
            return new Matrix(num, 0f, 0f, 0f, 0f, -num2, 0f, 0f, 0f, 0f, 1f, 0f, num, num2, 0f, 1f);
        }
    }
}

